from django import template

register = template.Library()

def device_getattr(value, arg):
    """
    Gets an attribute of an object dynamically from a string name
    Idea from: http://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template
    """

    if hasattr(value, str(arg)):
        a = getattr(value, arg)
        if a == None or a == '':
            if hasattr(value, 'model') and hasattr(value.model, arg):
                a = getattr(value.model, arg)
                if a == None or a == '':
                    a = ''
    else:
        a = ''

    return a

register.filter('device_getattr', device_getattr)

